import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _isString from "lodash/isString";
// @ts-ignore
import dynamic from 'dva/dynamic'; // @ts-ignore

import { createElement } from 'react';
export function getConvertRouter(_ref) {
  var cpaasRoutes = _ref.cpaasRoutes,
      _ref$getModels = _ref.getModels,
      getModels = _ref$getModels === void 0 ? function (e) {
    return e;
  } : _ref$getModels,
      _ref$getPages = _ref.getPages,
      getPages = _ref$getPages === void 0 ? function (e) {
    return e;
  } : _ref$getPages,
      options = _ref.options;

  var modelNotExisted = function modelNotExisted() {
    var app = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var model = arguments.length > 1 ? arguments[1] : undefined;
    return (// eslint-disable-next-line
      !(app._models || []).some(function (_ref2) {
        var namespace = _ref2.namespace;
        return namespace === model.substring(model.lastIndexOf('/') + 1);
      })
    );
  };

  function unWindRouteToRouter(_cpaasRoutes, convertRoutes) {
    var ndeep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;

    _cpaasRoutes.forEach(function (route, index) {
      var newRoute = _objectSpread({}, route); // delete newRoute.components;


      if (ndeep === 1) {
        newRoute.index = index === 0;
      }

      assignRouterData(newRoute, route); // eslint-disable-next-line no-param-reassign

      convertRoutes[route.path] = newRoute; // 子路由

      if (route.components !== undefined) {
        unWindRouteToRouter(route.components, convertRoutes, ndeep + 1);
        newRoute.notTrueRoute = true;
      }
    });
  }

  function assignRouterData(router, route) {
    var app = options.app;
    var models = route.models === undefined ? [] : route.models;

    if (route.component === undefined) {
      // @ts-ignore
      // eslint-disable-next-line no-param-reassign
      router.component = require("../routes").default;
    } else {
      var getRouterData = _isString(route.component) ? function () {
        return getPages(route.component);
      } : route.component;

      switch (route.type) {
        case 'vue':
        case 'react':
        default:
          // eslint-disable-next-line no-param-reassign
          router.component = dynamicWrapper(app, models, getRouterData);
          break;
      }
    }

    if (route.key !== undefined) {
      // eslint-disable-next-line no-param-reassign
      router.key = route.key;
    }

    if (route.authorized !== undefined) {
      // eslint-disable-next-line no-param-reassign
      router.authorized = route.authorized;
    }

    if (route.title !== undefined) {
      // eslint-disable-next-line no-param-reassign
      router.title = route.title;
    }

    if (route.icon !== undefined) {
      // eslint-disable-next-line no-param-reassign
      router.icon = route.icon;
    } // eslint-disable-next-line no-param-reassign


    router.exact = route.exact;
  } // wrapper of dynamic


  var dynamicWrapper = function dynamicWrapper(app) {
    var _models = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

    var _component = arguments.length > 2 ? arguments[2] : undefined;

    return dynamic({
      app: app,
      models: function models() {
        return _models.filter(function (item) {
          if (_isString(item) && !modelNotExisted(app, item)) {
            return false;
          } else {
            return true;
          }
        }).map(function (item) {
          // eslint-disable-next-line
          return _isString(item) ? getModels(item) : typeof item === 'function' ? item() : item;
        });
      },
      // add routerData prop
      component: function component() {
        // if (!routerDataCache) {
        //   routerDataCache = getRouterData(app);
        // }
        return _component().then(function (raw) {
          var Component = raw.default || raw;
          return function (props) {
            return createElement(Component, _objectSpread({}, props));
          };
        });
      }
    });
  };

  function convertRouter() {
    var convertRoutes = {};
    unWindRouteToRouter(cpaasRoutes, convertRoutes);
    return convertRoutes;
  }

  return convertRouter;
}